const url = require("url")
const querystring = require("querystring") 
const path = require('path')
const fs = require('fs')
const request = require('request')
const cheerio = require('cheerio')

const playerBase = 'http://www.ixxplayer.com/video.php'
const crawlBase = 'http://w.you-bbb.com'

const sleep = time => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, time)
  })
}

const rp = (url) => {
  return new Promise((resolve, reject) => {
    request({url: url, timeout: 5000 }, (error, response, body) => {
      if (error) {
        return reject(error)
      }
      resolve(body)
    })
  })
}

const downLoadTS = (downPath, savePath) => {
  return new Promise((resolve, reject) => {
    request(downPath, (error, response, body) => {
        if (error) {
          return reject(error)
        }
        resolve(body)
      })
      .pipe(fs.createWriteStream(savePath))
  })
}

;(async () => {
  // 分类爬取
  console.log(`------------[movie crawler run]------------`)
  for (let type = 1; type < 15; type++) {
    for(let page = 2; page > 1; page--) {
    // for(let page = 1; page < 7; page++) {
      let $ = null
      let result = []
      let crawlUrl = `${crawlBase}?m=vod-type-id-${type}-pg-${page}.html`
      await sleep(500)
      console.log(`开始爬取分类${type}的第${page}页`)
      await rp(crawlUrl)
        .then(data => {
          $ = cheerio.load(data)
        })
        .catch(err => {
      })

      if (!$ || !$('.thumb-overlay')) {
        console.log(`分类${type}的第${page}页不存在或者获取失败`)
        continue
      }

      $('.thumb-overlay').each((index, item) => {
        let $item = $(item).parent()
        let detailLink = $item.attr('href')
        let title =  $item.find('.video-title').text()
        let poster = $item.find('img').attr('src')
        result.push({ detailLink, title, poster, type})
      })
  
      for (let i = 0; i < result.length; i++) {
        $ = null
        await rp(crawlBase + result[i].detailLink)
          .then(data => {
            $ = cheerio.load(data)
          })
          .catch(err => {
        })
        if (!$) continue
        
        let flashContent = $('#flash').find('script').eq(0).html()
        if (!flashContent) continue
        let m3u8Url = flashContent.split("mac_url=unescape('")[1]
        if (m3u8Url) {
          m3u8Url = m3u8Url.split("')")[0]
          m3u8Url = unescape(m3u8Url).split('$')[1]
        }
        if (!m3u8Url) continue
        // 获取真实的M3U8地址
        let m3u8BaseUrl = url.parse(m3u8Url).protocol + '//' + url.parse(m3u8Url).host
        let oldPlayer = `${playerBase}?url=${m3u8Url}`
        result[i].m3u8Url = m3u8Url
        result[i].oldPlayer = oldPlayer
        let realM3U8 = ''
        await rp(m3u8Url)
          .then(data => {
            let Arr = data.split('\n')
            if (Arr.length > 7) {
              realM3U8 = m3u8Url
              return
            } else {
              realM3U8 = Arr[2]
              if (realM3U8.substring(0, 1) === '/') {
                realM3U8 = m3u8BaseUrl + realM3U8
                // result[i].id = realM3U8.split('ppvod/')[1].split('.')[0]
              } else {
                let base = m3u8Url.split('/index.m3u8')[0]
                realM3U8 = `${base}/${realM3U8}` 
                // result[i].id = base.split('/').pop()
              }
            }
            result[i].realM3U8 = realM3U8
            result[i].oldPlayer = `${playerBase}?url=${realM3U8}`
            // console.log(i + ' >>> ' + result[i].realM3U8)
          })
          .catch(err => {
            console.log(i + '>realM3u8 failed！')
        })
        // console.log(result[i])
        // 如果真实的M3U8地址获取到了 取出ts列表
        // if (!result[i].realM3U8) continue
        // let oldId = result[i].realM3U8.split('/').pop().split('.')[0]
        // result[i].oldId = oldId
        // let tsArr = []
        // await rp(result[i].realM3U8)
        //   .then(data => {
        //     console.log('tsList>>> success')
        //     let tsOld = data.split('\n')
        //     let len = tsOld.length
        //     for (let i = 0; i < len; i++) {
        //       if (tsOld[i].indexOf('.ts') > -1) {
        //         tsArr.push(m3u8BaseUrl + tsOld[i])
        //       }
        //     }
        //   })
        //   .catch(function (err) {
        //     console.log('tsList>>> fail')
        //   })
        
        // 下载ts文件
        // if (tsArr.length === 0) continue
        // let tsPath = path.resolve(process.cwd(), './server/resources/ts/' + oldId)
        // if (!fs.existsSync(tsPath)) fs.mkdirSync(tsPath)
        
        // for (let i = 0; i < tsArr.length; i++) {
        //   let downPath = tsArr[i]
        //   let fileName = downPath.split('/').pop()
        //   let savePath = path.resolve(__dirname, tsPath + '/' + fileName)
        //   await downLoadTS(downPath, savePath)
        //     .then(data => {
        //       console.log('saveTS success >>> ' + fileName)
        //     })
        //     .catch(err => {
        //       console.log('saveTS fail >>> ' + fileName)
        //     })
        // }
      }
      process.send({result})
    }
  }
  process.exit(0)
})()